import 'dart:convert';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:barcode_scan/barcode_scan.dart';
import '../home/home_list.dart';
import '../home/home_swiper.dart';
import '../goods/goods_detal.dart';
import '../home/home_detail.dart';
import '../config/api.dart';
import 'package:url_launcher/url_launcher.dart';
import '../provied/cartmodel.dart';
import '../drawer/drawer.dart';
class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);
//List data=[];
  @override
  _HomePageState createState() => _HomePageState();
}

void _getlist() async {
  try {
    Response response;
    response = await Dio().post('http://dbyxs.xyz:3005/goods/milei/get');

    // return print(response);
  } catch (e) {
    return print(e);
  }
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  TabController _tabController;
  String barcode;
  Futurescan() async {
    barcode = await BarcodeScanner.scan();
    _luancherurl(barcode);
     print(barcode + "内容");
  }
void _luancherurl(v) async {
    String url = v;
    //  String url='tel:'+'15862637996';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      print('访问失败');
    }
  }
  List u2 = [];
  Map<String, dynamic> map;

  var _username = new TextEditingController();
  FocusNode _focusNode = FocusNode();
  @override
  void initState() {
    super.initState();
     
    _tabController = new TabController(length: 10, vsync: this);
    _tabController.addListener(() {
   
    });
   // print('${this._tabController}切换');
    _focusNode.addListener(() {
       _focusNode.unfocus();
      if (!_focusNode.hasFocus) {
        _focusNode.unfocus();
        //  print('失去焦点');
        //  print(this._username.text);
      } else {
        _focusNode.unfocus();
        Navigator.of(context).pushNamed('/search');
        //  print('得到焦点');

      }
    });
    //  _username.text = '我是初始值';
  }

  List u = [];
  String u1;
  // getList(){
  //    getHomePageContent('http://dbyxs.xyz:3005/tion/get').then((value) => {
  //      // data= jsonDecode(value.toString())
  //     //  setState((){
  //     //    u1=value;
  //     //  })
  //   print(jsonDecode(value.toString()))
  //    });
  //   // print(this.u);
  //   // print('${this.u1['message']}没有数据吗');
  // }
  List data = [];
  // @override
  // void initState(){
  //   super.initState();
  // }
  // void _getDrawer(){
  //   return 
  // }
  int count;
  Widget build(BuildContext context) {
     ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);
    _getlist();
    return
        // DefaultTabController(length: 2, child:
        Scaffold(
           drawer: Drawer(child: Drawerpage(),),
            appBar: AppBar(
              title: Container(
                  //   padding: EdgeInsets.only(top: 10, bottom: 10),
                  height: 45,
                  // height: double.infinity,
                  // margin: EdgeInsets.only(bottom:10),
                  //color: Colors.blue[500],
                  child: Padding(
                      padding: EdgeInsets.only(
                        top: 5,
                      ),
                      child: TextField(
                        // maxLength: 1,
                        autocorrect: true,
                        // obscureText: true,
                        textAlign: TextAlign.start,

                        decoration: InputDecoration(
                            // icon: Icon(Icons.people),

                            // hintText: '输入搜索内容',
                            prefixIcon: IconButton(
                                icon: Icon(Icons.fullscreen),
                                onPressed: () {
                                  Futurescan();
                                  print('扫一扫');
                                }),
                            suffixIcon: IconButton(
                                icon: Icon(Icons.search),
                                onPressed: () {
                                 
                                  print('搜索');
                                }),
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(40),
                            ),
                            filled: true,
                            fillColor: Colors.white),
                        controller: _username,
                        focusNode: _focusNode,
                        onChanged: (value) {
                          setState(() {
                            this._username.text = value;
                          });
                        },
                      )
                      )
                      ),
              // leading: IconButton(
              //     icon: Icon(Icons.home),
              //     onPressed: () {
              //       print('我被点击了');
              //       //  getList();
              //       // print( getHomePageContent('http://dbyxs.xyz:3005/tion/get'));
              //     }),
              actions: <Widget>[
                IconButton(
                    icon: Icon(Icons.location_on),
                    onPressed: () {
                    //  Drawerpage();
                   // Scaffold.of(context).openDrawer();
                      Navigator.pushNamed(context, '/map');
                      print('点击搜索');
                    })
              ],
              bottom: TabBar(
                  labelColor: Colors.white,
                  // labelStyle: ,
                  isScrollable: true,
                  onTap: (value) {
                    setState(() {
                      this.count = value;
                    });
                  },
                  controller: this._tabController,
                  tabs: <Widget>[
                    Tab(text: '首页'),
                    Tab(text: '衣服'),
                    Tab(text: '女士'),
                    Tab(text: '内衣'),
                    Tab(text: '美食'),
                    Tab(text: '鞋品'),
                    Tab(text: '箱包'),
                    Tab(text: '手机'),
                    Tab(text: '保暖内衣'),
                    Tab(text: '数码家电'),
                  ]),
            ),
            // body:Text('你好')
            // body:HomeList(arguments:this.count)
            body:
                //  GestureDetector(
                //    behavior: HitTestBehavior.translucent,
                //    onTap: (){
                //      FocusScope.of(context).requestFocus(FocusNode());
                //    },
                //    child:
                IndexedStack(
                 // index:1,
            children: <Widget>[
                    Container(
              child: TabBarView(
                  controller: this._tabController,
                  children: <Widget>[
                    // HomeList(arguments:this.count),
                    Center(child: HomeSwiper()),
                    Center(child: GoodsDetal(arguments: {"id": 1})),
                    Center(child: GoodsDetal(arguments: {"id": 2})),
                    Center(child: GoodsDetal(arguments: {"id": 3})),
                    Center(child: GoodsDetal(arguments: {"id": 4})),
                    Center(child: GoodsDetal(arguments: {"id": 5})),
                    Center(child: GoodsDetal(arguments: {"id": 6})),
                    Center(child: GoodsDetal(arguments: {"id": 7})),
                    Center(child: GoodsDetal(arguments: {"id": 8})),
                    Center(child: GoodsDetal(arguments: {"id": 9})),
                    // Center(child: Text('女鞋')),
                  ]),
            ),
            Text('你还')
            ],
            ),
              floatingActionButton: FloatingActionButton(onPressed: (){
        Navigator.pushNamed(context, '/cart');
      },child: Icon(Icons.shopping_cart),backgroundColor: Colors.black38,),
            //   )
            // body: TabBarView(controller: this._tabController, children: <Widget>[
            //   Center(
            //     child: Text('手机'),
            //   ),
            //   Center(
            //     child: Text('电脑'),
            //   ),

            // ])
            // )
            );
  }
}
